var $ = getApp(), methods = require("../methods.js");
var interval = null;
Page(Object.assign({}, methods, {
	data: {
		tabnum: 1,
		buydialogHidden: true,
		num: 1,
		isfavorite: false,
		btntype: 1,
		ggselected: [],
		ks: '',
		gwcnum: 1,
		nodata: 0,
		indicatorDots: true,
		userinfo:[],
		current:0,
		isplay:0,
		djshour:'00',
		djsmin:'00',
		djssec:'00',
		showcuxiaodialog:false,
	},
	_onLoad: function () {
		var that = this
		var id = 0;
		if (that.options && that.options.id) {
			id = that.options.id
		}
		if (that.options && that.options.scene) {
			var scene = that.options.scene
			var scenes = scene.split('_');
			if(scenes[1]){
				id = parseInt(scenes[1]);
			}
		}
		$.post('shop/product', { id: id }, function (res) {
			if (res.status == 0) {
				$.alert(res.msg); return;
			}
			var product = res.product;
			var guigedata = res.guigedata;
			var guigelist = res.guigelist;
			var sysset = res.sysset;
			var isSeckill = res.isSeckill;
			var ggselected = [];
			for (var i = 0; i < guigedata.length; i++) {
				ggselected.push(0);
			}
			var ks = ggselected.join(',');
			//wxParse.wxParse('wxParseData', 'html', product.detail, that, 0);
			var pagecontent = JSON.parse(product.detail);
			var _bannerindex = [];
			for(var i in pagecontent){
				_bannerindex[i] = 0;
			}
			//var richtextarr = [];
			//for (var i = 0; i < pagecontent.length; i++) {
			//	if (pagecontent[i].temp == 'richtext') {
			//		var transData = wxParse.wxParse('richtext' + i, 'html', pagecontent[i].content, that, 0);
			//		pagecontent[i].content = transData.nodes;
			//	}
			//}
			sysset.textset = that.data.textset
			that.setData({
				business: res.business,
				product: product,
				cartnum:res.cartnum,
				commentlist:res.commentlist,
				commentcount:res.commentcount,
				cuxiaolist:res.cuxiaolist,
				couponlist:res.couponlist,
				pagecontent: pagecontent,
				_bannerindex:_bannerindex,
				guigelist: guigelist,
				guigedata: guigedata,
				ggselected: ggselected,
				ks: ks,
				sysset:sysset,
				shopset:res.shopset,
				isSeckill:isSeckill,
				nowtime:res.nowtime,
				seckill_duration:res.seckill_duration,
				seckill_starttime:res.guigelist[ks]['starttime'] ? res.guigelist[ks]['starttime'] : '',
				title:product.name,
				isfavorite:res.isfavorite,
				userinfo:res.userinfo,
				oglist:res.oglist,
				sharepic:product.pics[0]
			});
			clearInterval(interval);
			interval = setInterval(function(){
				that.setData({nowtime: that.data.nowtime+1})
				that.getdjs();
			},1000)

			wx.setNavigationBarTitle({
				title: product.name
			});
		})
	},
  swiperChange: function (e) {
		var that = this;
		that.setData({
			current: e.detail.current
		})
  }, 
	getdjs:function(){
		var that = this
		var nowtime = that.data.nowtime;
		var seckill_duration = that.data.seckill_duration;
		var seckill_starttime = that.data.seckill_starttime;
		var totalsec = seckill_starttime*1 + seckill_duration * 3600 - nowtime*1
		if(totalsec <=0){
			that.setData({djshour:'00',djsmin:'00',djssec:'00'})
		}else{
			var houer = Math.floor(totalsec/3600);
			var min = Math.floor((totalsec - houer *3600)/60);
			var sec = totalsec - houer*3600 - min*60
			var djs = (houer<10?'0':'')+houer+'时'+(min<10?'0':'')+min+'分'+(sec<10?'0':'')+sec+'秒';
			var djshour = (houer<10?'0':'')+houer
			var djsmin = (min<10?'0':'')+min
			var djssec = (sec<10?'0':'')+sec
			that.setData({djshour:djshour,djsmin:djsmin,djssec:djssec})
		}
	},
	payvideo:function(){
		this.setData({isplay:1})
		wx.createVideoContext('video').play();
	},
	parsevideo:function(){
		this.setData({isplay:0})
		wx.createVideoContext('video').stop();
	},
	//加入购物车
	buydialogShow: function (e) {
		var btntype = e.currentTarget.dataset.btntype;
		this.setData({
			btntype: btntype,
			buydialogHidden: !this.data.buydialogHidden
		})
	},
	buydialogChange: function (e) {
		this.setData({
			buydialogHidden: !this.data.buydialogHidden
		});
	},
	//选择规格
	ggchange: function (e) {
		var idx = e.currentTarget.dataset.idx;
		var itemk = e.currentTarget.dataset.itemk;
		var ggselected = this.data.ggselected
		ggselected[itemk] = idx
		var ks = ggselected.join(',');
		this.setData({
			ggselected: ggselected,
			ks: ks
		})
	},
	tobuy: function (e) {
		var that = this;
		var ks = that.data.ks;
		var proid = that.data.product.id;
		var ggid = that.data.guigelist[ks].id;
		var stock = that.data.guigelist[ks].stock;
		var num = that.data.gwcnum;
		if(num < 1) num = 1; 
		if(stock < num){
			$.error('库存不足');
			return ;
		}
		var prodata = proid + ',' + ggid + ',' + num;
		$.goto('buy?prodata=' + prodata);
	},
	//加入购物车操作
	addcart: function () {
		var that = this;
		var ks = that.data.ks;
		var num = that.data.gwcnum;
		var proid = that.data.product.id;
		var ggid = that.data.guigelist[ks].id;
		var stock = that.data.guigelist[ks].stock;
		if(num < 1) num = 1; 
		if(stock < num){
			$.error('库存不足');
			return ;
		}
		if(that.options && that.options.from == 'fastbuy'){
			$.post('shop/fastupdate', { type:'plus',proid: proid, ggid: ggid, num: num }, function (res) {
				setTimeout(function () {
					if (res.status == 1) {
						$.success('添加成功');
						that.setData({cartnum:res.cartnum})
					} else {
						$.error('请重试');
					}
				}, 300);
			}, '加载中');
		}else{
			$.post('shop/addcart', { proid: proid, ggid: ggid, num: num }, function (res) {
				setTimeout(function () {
					if (res.status == 1) {
						$.success('添加成功');
						that.setData({cartnum:res.cartnum})
					} else {
						$.error('请重试');
					}
				}, 300);
			}, '加载中');
		}
		that.setData({
			buydialogHidden: !that.data.buydialogHidden
		});
	},
	//加
	gwcplus: function (e) {
		var gwcnum = this.data.gwcnum + 1;
		var ks = this.data.ks;
		if (gwcnum > this.data.guigelist[ks].stock) {
			$.error('库存不足');
			return 1;
		}
		this.setData({
			gwcnum: this.data.gwcnum + 1
		})
	},
	//减
	gwcminus: function (e) {
		var gwcnum = this.data.gwcnum - 1;
		var ks = this.data.ks;
		if (gwcnum <= 0) {
			return;
		}
		this.setData({
			gwcnum: this.data.gwcnum - 1
		})
	},
	//输入
	gwcinput: function (e) {
		var ks = this.data.ks;
		var gwcnum = parseInt(e.detail.value);
		if (gwcnum < 1) return 1;
		if (gwcnum > this.data.guigelist[ks].stock) {
			return this.data.guigelist[ks].stock  > 0 ? this.data.guigelist[ks].stock : 1
		}
		this.setData({
			gwcnum: gwcnum
		});
	},
	//收藏操作
	addfavorite: function () {
		var that = this;
		var proid = that.data.product.id;
		$.post('shop/addfavorite', { proid: proid,type:'shop'}, function (data) {
			if (data.status == 1) {
				that.setData({ isfavorite: !that.data.isfavorite });
			}
			$.success(data.msg);
		}, '加载中');

	},
	tabClick: function (e) {
		this.setData({
			tabnum: e.currentTarget.dataset.num
		})
	},
	shareClick:function(){
		this.setData({sharetypevisible:true})
	},
	handleClickMask:function(){
		this.setData({sharetypevisible:false})
	},
	bindGetUserInfo: function (e) {
		var that = this
		var userInfo = e.detail.userInfo
		if (e.detail.userInfo) {
			$.post('pub/userinfo', { userInfo: userInfo,encryptedData:e.detail.encryptedData,iv: e.detail.iv}, function (data) {
				that.showPoster();
			});
		}
	},
	showPoster:function(){
		var that = this;
		that.setData({showposter:true,sharetypevisible:false});
		$.post('shop/getposter',{proid:that.data.product.id},function(data){
			if(data.status==0){
				$.alert(data.msg);
			}else{
				that.setData({posterpic:data.poster})
			}
		},'努力生成中')
	},
	posterDialogClose:function(){
		this.setData({showposter:false});
	},
	showcuxiaodetail:function(){
		this.setData({showcuxiaodialog:true})
	},
	hidecuxiaodetail:function(){
		this.setData({showcuxiaodialog:false})
	},
	onUnload:function(){
		clearInterval(interval);
	},
  onPageScroll:function(e){
    var that = this;
		var scrollY = e.scrollTop;// 获取垂直滚动的距离，即滚动了多少
		if (scrollY > 200 && !that.data.topbackhide){ //如果滚动距离大于550px则隐藏，否则删除隐藏类
			that.setData({topbackhide:true})
			that.setData({gotopshow:true})
		}
		if(scrollY < 150 && that.data.topbackhide){
			that.setData({topbackhide:false})
			that.setData({gotopshow:false})
		}
  },
	gotop:function(){
		wx.pageScrollTo({
			scrollTop: 0,
			duration: 500
		})
	},
}))